<template>
  <div class="lb-company-post">
    <top-nav />
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          icon="el-icon-plus"
          @click="addOrEditPosition(1)"
          >添加职位</lb-button
        >
      </el-row>
      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="职位名称" prop="keyword">
            <el-input
              v-model="searchForm.keyword"
              placeholder="请输入职位名称"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        v-loading="isLoading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="180"></el-table-column>
        <el-table-column
          prop="name"
          label="职位名称"
          width="180"
        ></el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <div>{{ scope.row.create_time | handleTime(1) }}</div>
            <div>{{ scope.row.create_time | handleTime(2) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="top" label="排序值"></el-table-column>
        <el-table-column prop="status  " label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updatePosition(scope.row.status, scope.row.id)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  addOrEditPosition(2, {
                    id: scope.row.id,
                    top: scope.row.top,
                    name: scope.row.name,
                    method: 2
                  })
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.page_count"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></lb-page>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="centerDialogVisible"
      width="500px"
      center
    >
      <el-form @submit.native.prevent :model="dialogForm" label-width="120px">
        <el-form-item label="职位名称">
          <el-input
            v-model="dialogForm.name"
            placeholder="请输入职位名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序值">
          <el-input-number
            class="lb-input-number"
            v-model.number="dialogForm.top"
            :controls="false"
            :precision="0"
            :min="0"
            placeholder="请输入排序值"
          ></el-input-number>
          <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">{{
          $t('action.cancel')
        }}</el-button>
        <el-button type="primary" @click="subPositionInfo">{{
          $t('action.comfirm')
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      isLoading: false,
      dialogTitle: '',
      centerDialogVisible: false,
      dialogForm: {
        id: '',
        name: '',
        top: 0,
        method: ''
      },
      searchForm: {
        keyword: '',
        page: 1,
        page_count: 10
      },
      total: 0,
      value: '1',
      tableData: []
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    /**
     * @method 获取职位列表
     */
    getTableDataList (flag = 0) {
      this.isLoading = true
      if (flag === 1) this.searchForm.page = 1
      let { searchForm } = this
      this.$api.getPositionList(searchForm).then(res => {
        this.isLoading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    /**
     * @method 上下架、删除职位
     */
    async updatePosition (status, id) {
      let opMethod = status === 2 ? 'del' : 'edit'
      let isW7 = this.$store.getters.isW7 === true ? '&' : '?'
      let opList = {
        // 上下架职位
        edit: {
          method: 'editPositionInfo',
          path: `${isW7}job_id=${id}`,
          param: { data: { status } }
        },
        // 删除公司
        del: {
          method: 'delPositionInfo',
          path: `${isW7}job_id=${id}`,
          param: {}
        }
      }
      let { code } = await this.$api[opList[opMethod].method](
        opList[opMethod].path,
        opList[opMethod].param
      )

      if (code === 200) {
        this.$message.success(
          this.$t(status === 2 ? 'tips.successDel' : 'tips.successOper')
        )
        if (status === 2) {
          this.searchForm.page =
            this.searchForm.page <
              Math.ceil((this.total - 1) / this.searchForm.page_count)
              ? this.searchForm.page
              : Math.ceil((this.total - 1) / this.searchForm.page_count)
          this.getTableDataList()
        }
      } else {
        if (status === 2) return
        this.getTableDataList()
      }
    },
    /**
     * @method 用户确认删除弹框
     */
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updatePosition(2, id)
      })
    },
    addOrEditPosition (flag, obj) {
      if (flag === 1) {
        this.dialogTitle = '添加职位'
        this.dialogForm = {
          id: '',
          name: '',
          top: 0,
          method: 1
        }
      } else {
        this.dialogTitle = '编辑职位'
        this.dialogForm = obj
      }
      this.centerDialogVisible = true
    },
    async subPositionInfo () {
      let { dialogForm } = this
      if (!dialogForm.name) {
        this.$message.error('请输入职位名称')
      } else if (dialogForm.top < 0) {
        this.$message.error('请输入正确的排序值')
      } else {
        /**
         * @method 新增/编辑职位
         * @author xiaoli
         */
        let isW7 = this.$store.getters.isW7 === true ? '&' : '?'
        let { id, name, top } = dialogForm
        let opMethod = id ? 'edit' : 'add'
        let opList = {
          // 新增职位
          add: {
            method: 'addPositionInfo',
            path: ''
          },
          // 编辑职位
          edit: {
            method: 'editPositionInfo',
            path: `${isW7}job_id=${id}`
          }
        }
        let { code } = await this.$api[opList[opMethod].method](
          opList[opMethod].path,
          { name, top }
        )
        if (code === 200) {
          this.$message.success(this.$t('tips.successOper'))
          this.centerDialogVisible = false
          this.dialogForm.name = ''
          this.dialogForm.top = ''
          this.getTableDataList()
        }
      }
    },
    handleSizeChange (val) {
      this.searchForm.page_count = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-company-post {
  width: 100%;
  .el-input-number,
  .el-input {
    width: 260px;
  }
}
</style>
